using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Meebey.SmartIrc4net;

namespace dotIRC
{
    public partial class DebugForm : IrcChildForm
    {
        public DebugForm(IrcSession session) : base(session)
        {
            InitializeComponent();
            //Activated+=new EventHandler(DebugForm_Activated);
            GotFocus += new EventHandler(DebugForm_GotFocus);
            FormClosing += new FormClosingEventHandler(this.DebugForm_FormClosing);
            irc.OnRawMessage += new IrcEventHandler(irc_OnRawMessage);
            irc.OnWriteLine += new WriteLineEventHandler(irc_OnWriteLine);
        }

        void irc_OnWriteLine(object sender, WriteLineEventArgs e)
        {
            if (InvokeRequired)
                BeginInvoke(new WriteLineEventHandler(irc_OnWriteLine), new object[] { sender, e });
            else
            {
                OutputBox.AppendText(e.Line + "\n");
                OutputBox.SelectionStart = OutputBox.Text.Length;
                OutputBox.ScrollToCaret();
            }
        }

        void irc_OnRawMessage(object sender, IrcEventArgs e)
        {
            if (InvokeRequired)
                BeginInvoke(new IrcEventHandler(irc_OnRawMessage), new object[] { sender, e });
            else
            {
                OutputBox.AppendText(e.Data.RawMessage + "\n");
                OutputBox.SelectionStart = OutputBox.Text.Length;
                OutputBox.ScrollToCaret();
            }
        }

        /*private void DebugForm_Activated(object sender, EventArgs e)
        {
            
        }*/

        void DebugForm_GotFocus(object sender, EventArgs e)
        {
            InputBox.Focus();
        }

        private void InputBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                irc.WriteLine(InputBox.Text);
                InputBox.Clear();
            }
        }

        private void DebugForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Since the IrcSession object still exists, manually unsubscribe from events.
            irc.OnRawMessage -= new IrcEventHandler(irc_OnRawMessage);
            irc.OnWriteLine -= new WriteLineEventHandler(irc_OnWriteLine);
        }
    }
}